<?php

class Sourcing_taskApp extends MemberbaseApp
{
	var $_sourcing_mod;
	var $_souritem_mod;
    function __construct()
    {
        $this->Sourcing_task();
    }
    function Sourcing_task()
    {
        parent::__construct();
		$this->_sourcing_mod =& m('sourcing');
		$this->_souritem_mod =& m('sourcingitem');
    }
	
    function index()
    {
		$user = $this->visitor->get();
		$this->check_powers(array(USER_SOURCING, USER_MANAGER));
		$search_status = empty($_GET['status']) ? 1 : intval($_GET['status']);
		$conditions = 'status = ' . $search_status;
		/*switch ($search_status) {
			case 1:
				$conditions = 'sourcing_list.to_sourcing_date > 0 AND sourcing_accept = 0';
				break;	//采购确认
			case 2:
				$conditions = 'sourcing_list.sourcing_accept > 0 AND finish_date = 0';
				break;	//采购中
			case 3:
				$conditions = 'sourcing_list.finish_date > 0 AND confirm_fabric_date = 0';
				break;	//跟单确认
			case 4:
				$conditions = 'sourcing_list.confirm_fabric_date > 0';
				break;	//采购完成
		}*/
		if ($user['role'] == USER_SOURCING) {
			$conditions .= ' AND sourcing_assistant_id = ' . $user['user_id'];
		}
		
		$page = $this->_get_page();
		$sourcing_list = $this->_sourcing_mod->find(array(
			'conditions' => $conditions . ' AND to_sourcing_date > 0',
			'order' => 'list_sn DESC',
			'join' => 'belongs_sourcing',
			'count' => true,
			'limit' => $page['limit']
		));
		$page['item_count'] = $this->_sourcing_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		foreach ($sourcing_list as $key => $item) {
			$sourcing_list[$key]['sourcing_assistant'] = $item['user_name'];
			if ($item['type'] == SOURCING_LIST_TYPE_SAMPLE) {
				$sample_mod =& m('sample');
				$sample = $sample_mod->get(array(
					'fields' => 'sample.sample_sn, sample.arrival_date, brand.brand_name, member.user_name, sample.ref_sample_style, sample.front_pic',
					'conditions' => 'sample_id = ' . $item['key_id'],
					'join' => 'belongs_brand,belongs_merchandiser',
				));
				foreach ($sample as $k => $value) {
					$sourcing_list[$key][$k] = $value;
				}
			} else {
				$order_mod =& m('order');
				$order = $order_mod->get_info($item['key_id']);
				foreach ($order as $k => $value) {
					$sourcing_list[$key][$k] = $value;
				}
			}
		}
		
		$this->assign('user', $user);
		$this->assign('search_status', $search_status);
		$this->assign('sourcing_list', $sourcing_list);
        $this->display('sourcing_task.index.html');
		$this->_curlocal(Lang::get('sourcing_task'));
    }
	
	function edit()
	{
		$this->check_powers(array(USER_SOURCING, USER_MANAGER));
		
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id) {
			$this->show_warning('sourcing_task_not_exist');
			return;
		}
		$sourcing_task = current($this->_sourcing_mod->find(array(
			'conditions' => 'list_id = ' . $id,
			'join' => 'belongs_sourcing',
		)));
		if (!$sourcing_task) {
			$this->show_warning('sourcing_task_not_exist');
			return;
		}
		if ($sourcing_task['sourcing_assistant_id'] != $this->visitor->get('user_id')) {
			$this->show_warning('sourcing_task_not_exist');
			return;
		}
		
		if (!IS_POST) {
			$sourcing_task['sourcing_assistant'] = $sourcing_task['user_name'];
			if ($sourcing_task['type'] == SOURCING_LIST_TYPE_SAMPLE) {
				$sample_mod =& m('sample');
				$sample = current($sample_mod->find(array(
					'conditions' => 'sample_id = ' . $sourcing_task['key_id'],
					'join' => 'belongs_brand,belongs_merchandiser',
				)));
				foreach ($sample as $k => $value) {
					$sourcing_task[$k] = $value;
				}
			} else {
				$order_mod =& m('order');
				$order = $order_mod->get_info($sourcing_task['key_id']);
				foreach ($order as $k => $value) {
					$sourcing_task[$k] = $value;
				}
			}
			
			$fabric_mod =& m('fabric_shop');
			$this->assign('fabric_shop_list', $fabric_mod->find(array('conditions'=>'if_show = 1', 'order'=>'fabric_shop_name ASC')));
			
			$sourcing_task['item'] = $this->_souritem_mod->find(array(
				'conditions' => 'list_id = ' . $id,
				'join' => 'belongs_fabric_shop',
			));
			
			$upload_mod =& m('upload');
			$sourcing_task['files'] = $upload_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_SOURCING_LIST . ' AND key_id = ' . $id,
			));
			
			$this->assign('belongs_sourcing_list', BELONGS_SOURCING_LIST);
			
			$this->assign('sourcing_task', $sourcing_task);
			$this->display('sourcing_task.form.html');
			$this->_curlocal(Lang::get('sourcing_task'), 'index.php?app=sourcing_task', Lang::get('more_info'));
		} else {
			$item_id = empty($_POST['item_id']) ? 0 : intval($_POST['item_id']);
			if (!$item_id) {
				$this->show_warning('sourcing_item_not_exist');
				return;
			}
			$fabric_info = trim($_POST['fabric_info']);
			$fabric_shop_id = intval($_POST['fabric_shop_id']);
			
			$data = array(
				'fabric_info' => $fabric_info,
				'fabric_shop_id' => $fabric_shop_id,
			);
			
			$this->_souritem_mod->edit($item_id, $data);
			if ($this->_souritem_mod->get_error()) {
				$this->show_warning($this->_souritem_mod->get_error());
				return;
			}
			$this->show_message('edit_sourcing_info_success', 'sourcing_task', 'index.php?app=sourcing_task&act=edit&id=' . $id);
			//print_r($_POST);
		}
		//print_r($sourcing_task);
	}
	
	function accept()
	{
		$this->check_powers(array(USER_SOURCING));
		
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id) {
			$this->show_warning('sourcing_task_not_exist');
			return;
		}
		$sourcing_task = $this->_sourcing_mod->get_info($id);
		if (!$sourcing_task) {
			$this->show_warning('sourcing_task_not_exist');
			return;
		}
		if ($sourcing_task['sourcing_assistant_id'] != $this->visitor->get('user_id')) {
			$this->show_warning('no_powers_to_visit');
			return;
		}
		
		if ($sourcing_task['type'] == SOURCING_LIST_TYPE_SAMPLE) {
			$sample_mod =& m('sample');
			$sample = $sample_mod->get_info($sourcing_task['key_id']);
			if ($sample['status'] != SAMPLE_STATUS_SOUR_CONFIRM) {
				$this->show_warning('sourcing_task_not_exist');
				return;
			}
		}
		
		if ($sourcing_task['status'] > SAMPLE_STATUS_SOUR_CONFIRM) {
			$this->show_warning('sourcing_task_is_accepted');
			return;
		}
		
		$data = array(
			'sourcing_accept' => $sourcing_task['sourcing_accept'] ? $sourcing_task['sourcing_accept'] : time(),
			'status' => SAMPLE_STATUS_SOURCING
		);
		$this->_sourcing_mod->edit($id, $data);
		if ($this->_sourcing_mod->get_error()) {
			$this->_error($this->_sourcing_mod->get_error());
			return;
		}
		
		$this->_souritem_mod->edit('list_id = ' . $id . ' AND status < ' . SOURITEM_STATUS_SOURCING, array('status'=>SOURITEM_STATUS_SOURCING));
		
		if ($sourcing_task['type'] == SOURCING_LIST_TYPE_SAMPLE) {
			$sample_mod->edit($sourcing_task['key_id'], array('status' => SAMPLE_STATUS_SOURCING));
			if ($sample_mod->get_error()) {
				$this->_error($sample_mod->get_error());
				return;
			}
		}
		
		$next_task = $this->_sourcing_mod->get(array(
			'conditions' => "list_sn <> '$sourcing_task[list_sn]' AND sourcing_assistant_id = " . $this->visitor->get('user_id') . ' AND sourcing_accept = 0',
			'order' => 'list_sn ASC'
		));
		if (!$next_task) {
			$this->show_message('accept_sourcing_task_success', 'sourcing_task', 'index.php?app=sourcing_task&status=2');
			return;
		}
		
		$this->show_message('accept_sourcing_task_success', 'sourcing_task', 'index.php?app=sourcing_task&act=edit&id=' . $next_task['list_id']);
	}
	
	function print_()
	{
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id) {
			$this->show_warning('sourcing_task_not_exist');
			return;
		}
		
		$sourcing_task = $this->_sourcing_mod->get_info($id);
		if (!$sourcing_task) {
			$this->show_warning('sourcing_task_not_exist');
			return;
		}
		
		if (!$sourcing_task['sourcing_accept']) {
			$this->show_warning('accept_pls');
			return;
		}
		if ($sourcing_task['finish_date']) {
			$this->show_warning('sourcing_task_finished');
			return;
		}
		
		$sourcing_task['item'] = $this->_souritem_mod->find(array(
			'conditions' => 'list_id = ' . $sourcing_task['list_id'],
			'join' => 'belongs_fabric_shop',
		));
		
		if ($sourcing_task['type'] == SOURCING_LIST_TYPE_SAMPLE) {
			$sample_mod =& m('sample');
			$sample = $sample_mod->get(array(
				'conditions' => 'sample_id = ' . $sourcing_task['key_id'],
				'join' => 'belongs_merchandiser',
			));
			foreach ($sample as $key => $value) {
				$sourcing_task[$key] = $value;
			}
		} else {
			$order_mod =& m('order');
			$order = $order_mod->get_info($sourcing_task['key_id']);
			foreach ($order as $key => $value) {
				$sourcing_task[$key] = $value;
			}
		}
		
		$this->assign('sourcing_task', $sourcing_task);
		$this->display('sourcing_task.print_.html');
		//$this->_curlocal(Lang::get('sourcing_task'), 'index.php?app=sourcing_task', Lang::get('print_sourcing_list'));
	}
	
	function finish()
	{
		$this->check_powers(array(USER_SOURCING));
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id) {
			$this->show_warning('sourcing_task_not_exist');
			return;
		}
		
		$sourcing_task = $this->_sourcing_mod->get_info($id);
		if (!$sourcing_task) {
			$this->show_warning('sourcing_task_not_exist');
			return;
		}
		if ($sourcing_task['sourcing_assistant_id'] != $this->visitor->get('user_id')) {
			$this->show_warning('sourcing_task_not_exist');
			return;
		}
		
		if ($sourcing_task['type'] == SOURCING_LIST_TYPE_SAMPLE) {
			$sample_mod =& m('sample');
			$sample = $sample_mod->get_info($sourcing_task['key_id']);
			if ($sample['status'] != SAMPLE_STATUS_SOURCING) {
				$this->show_warning('sourcing_task_not_exist');
				return;
			}
		}
		
		$data = array(
			'finish_date' => time(),
			'status' => SAMPLE_STATUS_MERCH_CONFIRM
		);
		$this->_sourcing_mod->edit($id, $data);
		if ($this->_sourcing_mod->get_error()) {
			$this->_error($this->_sourcing_mod->get_error());
			return;
		}
		$this->_souritem_mod->edit('list_id = ' . $id . ' AND status < ' . SOURITEM_STATUS_MERCH_CONFIRM, array('status'=>SOURITEM_STATUS_MERCH_CONFIRM));
		
		if ($sourcing_task['type'] == SOURCING_LIST_TYPE_SAMPLE) {
			$sample_mod->edit($sourcing_task['key_id'], array('status' => SAMPLE_STATUS_MERCH_CONFIRM));
			if ($sample_mod->get_error()) {
				$this->_error($sample_mod->get_error());
				return;
			}
		}
		$this->_sourcing_mod->edit($sourcing_task['list_id'], array('status'=>SAMPLE_STATUS_MERCH_CONFIRM));
		$this->show_message('finish_success', 'sourcing_task', 'index.php?app=sourcing_task');
	}
	
	function show()
	{
		$this->check_powers(array(USER_MANAGER));
		
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id) {
			$this->show_warning('sourcing_task_not_exist');
			return;
		}
		$sourcing_task = current($this->_sourcing_mod->find(array(
			'conditions' => 'list_id = ' . $id,
			'join' => 'belongs_sourcing',
		)));
		if (!$sourcing_task) {
			$this->show_warning('sourcing_task_not_exist');
			return;
		}
		
		$sourcing_task['sourcing_assistant'] = $sourcing_task['user_name'];
		if ($sourcing_task['type'] == SOURCING_LIST_TYPE_SAMPLE) {
			$sample_mod =& m('sample');
			$sample = current($sample_mod->find(array(
				'conditions' => 'sample_id = ' . $sourcing_task['key_id'],
				'join' => 'belongs_brand,belongs_merchandiser',
			)));
			foreach ($sample as $k => $value) {
				$sourcing_task[$k] = $value;
			}
		} else {
			$order_mod =& m('order');
			$order = $order_mod->get_info($sourcing_task['key_id']);
			foreach ($order as $k => $value) {
				$sourcing_task[$k] = value;
			}
		}
			
		$sourcing_task['item'] = $this->_souritem_mod->find(array(
			'conditions' => 'list_id = ' . $id,
			'join' => 'belongs_fabric_shop',
		));
			
		$upload_mod =& m('upload');
		$sourcing_task['files'] = $upload_mod->find(array(
			'conditions' => 'file_type = ' . BELONGS_SOURCING_LIST . ' AND key_id = ' . $id,
		));
		
		$this->assign('sourcing_task', $sourcing_task);
		$this->display('sourcing_task.show.html');
		$this->_curlocal(Lang::get('sourcing_task'), 'index.php?app=sourcing_task', Lang::get('more_info'));
	}
	
	function print_qd()
	{
		if (!IS_POST) {
			$ids = $_GET['ids'];
			if (!$ids) {
				$this->show_warning('sourcint_task_select_pls');
				return;
			}
			//$ids = implode(',', $ids);
			$list = $this->_sourcing_mod->find(array(
				'conditions' => "list_id in($ids)" . ' AND list_sn != \'\'',
				'order' => 'list_sn DESC',
			));
			foreach ($list as $key => $item) {
				if ($item['type'] == SOURCING_LIST_TYPE_SAMPLE) {
					$sample_mod =& m('sample');
					$sample = $sample_mod->get(array(
						'fields' => 'sample.sample_sn, sample.arrival_date, member.user_name, sample.ref_sample_style',
						'conditions' => 'sample_id = ' . $item['key_id'],
						'join' => 'belongs_merchandiser',
					));
					foreach ($sample as $k => $value) {
						$list[$key][$k] = $value;
					}
				} else {
					$order_mod =& m('order');
					$order = $order_mod->get_info($item['key_id']);
					foreach ($order as $k => $value) {
						$list[$key][$k] = $value;
					}
				}
				$list[$key]['item'] = $this->_souritem_mod->find(array(
					'conditions' => 'list_id = ' . $item['list_id'],
					'order' => 'item_id ASC',
				));
				$list[$key]['countitem'] = count($list[$key]['item']) - 1;
			}
			$this->assign('list', $list);
			$this->display('sourcing_task.print_qd.html');
			//$this->_curlocal(Lang::get('sourcing_task'), 'index.php?app=sourcing_task', '打印采购清单');
			//print_r($list);
		}
	}
}

?>
